<template>
  <div class="app-container">
    <el-button
      type="primary"
      @click="addSegment"
      v-if="$store.getters.curRole == 'schoolAdmin'"
    >新增节次
    </el-button
    >
    <div class="tips">注：时间不可重合</div>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="节次" align="center" prop="attendanceName">
      </el-table-column>
      <el-table-column label="节次时间范围" align="center" prop="time">
        <template slot-scope="scope">
          {{ scope.row.beginTime }} - {{ scope.row.endTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="updateSegment(scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-show="scope.$index !== 0"
            @click="moveSegment(scope, 1)"
          >上移
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-show="scope.$index !== tableData.length - 1"
            @click="moveSegment(scope, 0)"
          >下移
          </el-button>
          <el-button size="mini" type="text" @click="deleteSegment(scope)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getSegmentData"
    />
    <editSegment
      ref="editSegment"
      :open.sync="editSegmentOpen"
      :item="editSegmentItem"
      :type="editSegmentType"
      @refresh="getSegmentData"
    ></editSegment>
  </div>
</template>

<script>
import {
  listattendance,
  moveAttendance,
  deleteAttendance
} from '@/api/configuration/attendance'
import editSegment from '@/views/configuration/components/editSegment.vue'

export default {
  name: 'SegmentAllocation',
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      tableData: [],
      loading: false,
      editSegmentOpen: false,
      editSegmentItem: {},
      editSegmentType: 'add'
    }
  },
  components: { editSegment },
  mounted() {
    this.getSegmentData()
  },
  methods: {
    async getSegmentData() {
      this.loading = true
      try {
        let res = await listattendance({
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize,
          schoolId: this.$store.getters.schoolId,
          branchId: this.$store.getters.branchId
        })
        const { code, rows, total } = res
        if (code == 200) {
          this.total = total
          this.tableData = rows
        }
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    },
    addSegment() {
      this.editSegmentOpen = true
      this.editSegmentType = 'add'
    },
    updateSegment(data) {
      this.editSegmentType = 'edit'
      this.editSegmentOpen = true
      this.editSegmentItem = data
    },
    deleteSegment(scope) {
      this.$confirm(
        `确定删除【${scope.row.attendanceName}】吗？删除后教师将无法在该时段进行考勤操作，且此操作不可撤销！`,
        '删除确认',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }
      )
        .then(async() => {
          this.loading = true
          try {
            let res = await deleteAttendance(scope.row.id)
            const { code } = res
            if (code == 200) {
              this.getSegmentData()
              this.$message({
                type: 'success',
                message: '删除成功'
              })
            }
          } catch (error) {
            console.error(error)
          } finally {
            this.loading = false
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async moveSegment(scope, type) {
      let data = JSON.parse(JSON.stringify(scope.row))
      this.loading = true

      try {
        let res
        // type:1 上移 0 下移
        if (type == 1) {
          data.move = -1
          res = await moveAttendance(data)
          this.tableData.splice(
            scope.$index - 1,
            0,
            this.tableData.splice(scope.$index, 1)[0]
          )
        } else {
          data.move = 1
          res = await moveAttendance(data)

          this.tableData.splice(
            scope.$index + 1,
            0,
            this.tableData.splice(scope.$index, 1)[0]
          )
        }
        const { code } = res
        if (code == 200) {
          this.$message({
            type: 'success',
            message: `${type == 1 ? '上移' : '下移'}成功`
          })
          this.loading = false
        }
      } catch (error) {
        console.error(error)
        this.loading = false
      } finally {

      }
    }
  }
}
</script>

<style scoped lang="scss">
.tips {
  margin: 20px 0;
  font-size: 14px;
  color: orange;
}
</style>
